/**
 * @file:          Svpwm.c
 * @brief:
 * @details:
 * @author:        wjh
 * @date created:  2025.07.06
 * @version:       1.0.0
 * @par copyright (c):
 *
 * @par history (desc):
 *   version:1.0.0, wjh, 2025.07.06,17:39:22
 */

/* Include Files **************************************************************/
#include "svpwm.h"

/* Global Variable Define *****************************************************/

/* Function Define ************************************************************/

void Svpwm_Init(struct Svpwm *self)
{
    self->Run = Svpwm_Run_7Segment;
}

// 七段式SVPWM核心算法
void Svpwm_Run_7Segment(struct Svpwm *self)
{
    float sqrt3 = bm_sqrt_carmack(3.0f);

    // 1. 计算电压
    self->Ux = self->in_ubeta;
    self->Uy = 0.5f * (sqrt3 * self->in_ualpha - self->in_ubeta);
    self->Uz = -0.5f * (sqrt3 * self->in_ualpha + self->in_ubeta);

    // 2. 扇区判断 (基于三相电压符号) <cite data-id='160020'>160020</cite>
    int A = (self->Ux > 0) ? 1 : 0;
    int B = (self->Uy > 0) ? 1 : 0;
    int C = (self->Uz > 0) ? 1 : 0;
    self->N = A + 2 * B + 4 * C; // N=1~6对应6个扇区
    self->sector = N2Section[self->N];

    // 3. 计算矢量作用时间 (标幺化) <cite data-id='10003'>10003</cite>
    self->Tx = sqrt3 * self->param_Ts * self->Ux / self->param_Udc;
    self->Ty = sqrt3 * self->param_Ts * -self->Uz / self->param_Udc; // 0.5 * (sqrt3 * self->in_ualpha + self->in_ubeta);
    self->Tz = sqrt3 * self->param_Ts * -self->Uy / self->param_Udc; // 0.5 * (-sqrt3 * self->in_ualpha + self->in_ubeta);

    switch (self->N)
    {
    case 1:
        self->T1 = self->Tz;
        self->T2 = self->Ty;
        break;
    case 2:
        self->T1 = self->Ty;
        self->T2 = -self->Tx;
        break;
    case 3:
        self->T1 = -self->Tz;
        self->T2 = self->Tx;
        break;
    case 4:
        self->T1 = -self->Tx;
        self->T2 = self->Tz;
        break;
    case 5:
        self->T1 = self->Tx;
        self->T2 = -self->Ty;
        break;
    case 6:
        self->T1 = -self->Ty;
        self->T2 = -self->Tz;
        break;
    default: // 错误处理
        self->T1 = 0;
        self->T2 = 0;
        break;
    }
    self->T0 = self->param_Ts - self->T1 - self->T2; // 零矢量时间

    /* 过调制处理 */
    if (self->T1 + self->T2 > self->param_Ts)
    {
        self->T1_sat = self->param_Ts * self->T1 / (self->T1 + self->T2);
        self->T2_sat = self->param_Ts * self->T2 / (self->T1 + self->T2);
        self->T0_sat = 0.0F;
    }else
    {
        self->T1_sat = self->T1;
        self->T2_sat = self->T2;
        self->T0_sat = self->T0;
    }
    

    self->Ta = (self->param_Ts - self->T1_sat - self->T2_sat) / 4;
    self->Tb = self->Ta + self->T1_sat / 2;
    self->Tc = self->Tb + self->T2_sat / 2;

    // 4. 七段式PWM波形合成
    switch (self->N)
    {
    case 1:
        self->out_Tcm1 = self->Tb;
        self->out_Tcm2 = self->Ta;
        self->out_Tcm3 = self->Tc;
        break;
    case 2:
        self->out_Tcm1 = self->Ta;
        self->out_Tcm2 = self->Tc;
        self->out_Tcm3 = self->Tb;
        break;
    case 3:
        self->out_Tcm1 = self->Ta;
        self->out_Tcm2 = self->Tb;
        self->out_Tcm3 = self->Tc;
        break;
    case 4:
        self->out_Tcm1 = self->Tc;
        self->out_Tcm2 = self->Tb;
        self->out_Tcm3 = self->Ta;
        break;
    case 5:
        self->out_Tcm1 = self->Tc;
        self->out_Tcm2 = self->Ta;
        self->out_Tcm3 = self->Tb;
        break;
    case 6:
        self->out_Tcm1 = self->Tb;
        self->out_Tcm2 = self->Tc;
        self->out_Tcm3 = self->Ta;
        break;
    default:
        self->out_Tcm1 = self->Ta;
        self->out_Tcm2 = self->Tb;
        self->out_Tcm3 = self->Tc;
        break;
    }
}
